clear
est clear

cap mat define migrationfigure=J(105,10,.)
		local row=1

	use "$input/full_geoid_pair_panel.dta"
	
	
			
	replace migration=migration/(tot_pop/100000)

	//Make sure only include states that have a storm in at least one county at least once in our sample years
	bysort state_fips: egen max=max(storm)
		drop if max!=1
		
	//Create future pooled migration - total migrants in the subsequent five years
	sort geoid_pair move_year
	g post_storm=1 if storm==1|storm[_n-1]==1&geoid_pair==geoid_pair[_n-1]|storm[_n-2]==1&geoid_pair==geoid_pair[_n-2]|storm[_n-3]==1&geoid_pair==geoid_pair[_n-3]|storm[_n-4]==1&geoid_pair==geoid_pair[_n-4]|storm[_n-5]==1&geoid_pair==geoid_pair[_n-5]
	mvencode post_storm, mv(0)
	
	//Collapse to total migration from a county (not looking at paired county to county migration, just all migration out of a county)
	collapse (sum) migration (mean) share_approved totalapprovedihpamount,by(from_geoid move_year storm post_storm state_fips)
	
	//Create IHS transformations
	ihstrans(migration)
	
	
	//Create lags of storms (was there a storm in t-1 years)

			
	label var post_storm "1(0-5 yrs post storm)"
	label var storm "1(Storm year)"
	sort from_geoid move_year
	
	replace totalap=totalap/1000000
	g highdamage=(totalapp>10) if totalapp!=.
	mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
	foreach var in L1 L2 L3 L4 L5{
		label var highdamage_`var' "1(Storm year), `var'"
	}
		
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
		
			
			eststo t1c1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="no lags, net"
			local row=`row'+1
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L1]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L1]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L2]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L2]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L3]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L3]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L4]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L4]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L5]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L5]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta11=trim("`: display %10.3f r(estimate)'")
				local se11=trim("`: display %10.3f r(se)'")	
				cap mat def migrationfigure[`row',1]=`beta11'
				cap mat def migrationfigure[`row',2]=`se11'
				cap mat def migrationfigure[`row',3]="lags, out"
				local row=`row'+1	
		
		drop highdamage*
		g highdamage=(totalapp>20) if totalapp!=.
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			

			eststo t1c2
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="no lags, net"
			local row=`row'+1
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c2
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L1]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L1]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L2]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L2]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L3]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L3]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L4]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L4]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L5]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L5]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta21=trim("`: display %10.3f r(estimate)'")
				local se21=trim("`: display %10.3f r(se)'")	
				cap mat def migrationfigure[`row',1]=`beta21'
				cap mat def migrationfigure[`row',2]=`se21'
				cap mat def migrationfigure[`row',3]="lags, out"
				local row=`row'+1	
		
		drop highdamage*
		g highdamage=(totalapp>40) if totalapp!=.
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			

		eststo t1c3
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="no lags, net"
			local row=`row'+1
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c3
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L1]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L1]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L2]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L2]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L3]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L3]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L4]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L4]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L5]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L5]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta31=trim("`: display %10.3f r(estimate)'")
				local se31=trim("`: display %10.3f r(se)'")	
				cap mat def migrationfigure[`row',1]=`beta31'
				cap mat def migrationfigure[`row',2]=`se31'
				cap mat def migrationfigure[`row',3]="lags, out"
				local row=`row'+1	
		
		drop highdamage*
		g highdamage=(totalapp>80) if totalapp!=.
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)

		eststo t1c4
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="no lags, net"
			local row=`row'+1
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c4
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L1]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L1]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L2]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L2]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L3]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L3]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L4]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L4]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L5]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L5]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta41=trim("`: display %10.3f r(estimate)'")
				local se41=trim("`: display %10.3f r(se)'")	
				cap mat def migrationfigure[`row',1]=`beta41'
				cap mat def migrationfigure[`row',2]=`se41'
				cap mat def migrationfigure[`row',3]="lags, out"
				local row=`row'+1
		
		
		drop highdamage*
		g highdamage=(totalapp>160) if totalapp!=.
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
		

		eststo t1c5
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="no lags, net"
			local row=`row'+1
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c5
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L1]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L1]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L2]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L2]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L3]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L3]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L4]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L4]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage_L5]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage_L5]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta51=trim("`: display %10.3f r(estimate)'")
				local se51=trim("`: display %10.3f r(se)'")	
				cap mat def migrationfigure[`row',1]=`beta51'
				cap mat def migrationfigure[`row',2]=`se51'
				cap mat def migrationfigure[`row',3]="lags, out"
				local row=`row'+1	

label var highdamage "1(Storm year)"				
				
	file open 	t 		using "$tables/table_si6.tex", replace write
	file write t	"\begin{table}[htbp]\footnotesize \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
						"\caption{Migration impacts of high damage storms}\label{tab: total migration effects high damage}" _n  ///
						"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{10}{c}}" _n "\midrule" _n ///
						"&$>10MM$&$>20MM$&$>40MM$&$>80MM$&$>160MM$&\\" _n ///
						"\midrule" _n 
	file close 	t
	
				esttab t1c* using "$tables/table_si6.tex", l keep(1.highdamage) ///
				s(N, l("N") f(%11.0fc) lay(@)) $opts 
				
	file open 	t 		using "$tables/table_si6.tex", append write
	file write t "\\" ///
							"\midrule																			" ///
							"Fixed Effects:									&		&		&		&		&					\\" ///
							"\hspace{3mm}County								&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\hspace{3mm}Year								&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\hspace{3mm}State\$\times\$Year				&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\midrule" _n "\end{tabular*}" _n ///
							"\begin{tabular*}{1\textwidth}{p{6in}}" _n ///
							"\footnotesize \textsc{Notes:} All columns report the results of a fixed effects specification with the IHS transformation of the number of out-migrants from a county as the outcome. Counties are defined as exposed to a storm if at least one hurricane resulted in a flood warning during the year or the county experienced wind speeds of at least 21 m/s - the speed at which structural damage begins to occur - during a hurricane in the year and the total value of compensation approved by FEMA was greater than indicated in the column heading. Roughly 90\% of storms in our sample result in less than \$10 million in approved compensation. 95\% result in less than \$20 million in approved compensation. \$^{*}\$p=0.1, \$^{**}\$p=0.05,\$^{***}\$p=0.01." ///
							"\end{tabular*}" _n "\end{table}" _n 
	file close 	t		
	
	foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
			
	file open 	t 		using "$tables/table_si7.tex", replace write
	file write t	"\begin{table}[htbp]\footnotesize \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
						"\caption{Lagged migration impacts of high damage storms}\label{tab: lagged highdamage}" _n  ///
						"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{10}{c}}" _n "\midrule" _n ///
						"&$>10MM$&$>20MM$&$>40MM$&$>80MM$&$>160MM$&\\" _n ///
						"\midrule" _n 
	file close 	t
	
				esttab t2c* using "$tables/table_si7.tex", l keep(1.highdamage 1.highdamage_L* ) ///
				s(N, l("N") f(%11.0fc) lay(@)) $opts 
				
	file open 	t 		using "$tables/table_si7.tex", append write
	file write t "\\" ///
							"\midrule																			" ///
							"\$\Sigma_{i=0}^{5}Storm_{t-{i}}=1\$			&`beta11'&`beta21'&`beta31'&`beta41'&`beta51'				\\" ///
							"												&(`se11')&(`se21')&(`se31')&(`se41')&(`se51')				\\"	///
							"\midrule																		" ///
							"Fixed Effects:									&		&		&		&		&			\\" ///
							"\hspace{3mm}County								&Yes	&Yes	&Yes	&Yes	&Yes		\\ " ///
							"\hspace{3mm}Year								&Yes	&Yes	&Yes	&Yes	&Yes		\\ " ///
							"\hspace{3mm}State\$\times\$Year				&Yes	&Yes	&Yes	&Yes	&Yes		\\ " ///
							"\midrule" _n "\end{tabular*}" _n ///
							"\begin{tabular*}{1\textwidth}{p{6.5in}}" _n ///
							"\footnotesize \textsc{Notes:} All columns report the results of a fixed effects specification with the IHS transformation of the number of out-migrants from a county as the outcome. Counties are defined as exposed to a storm if at least one hurricane resulted in a flood warning during the year or the county experienced wind speeds of at least 21 m/s - the speed at which structural damage begins to occur - during a hurricane in the year and the total value of compensation approved by FEMA was greater than indicated in the column heading. Roughly 90\% of storms in our sample result in less than \$10 million in approved compensation. 95\% result in less than \$50 million in approved compensation. 1(Storm year), t-\# indicates that the county experienced a storm \# years previously. \$^{*}\$p=0.1, \$^{**}\$p=0.05,\$^{***}\$p=0.01."  ///
							"\end{tabular*}" _n "\end{table}" _n 
	file close 	t	
	
	
	
	
	
	
			
			
			